<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function sum(a, b) {
        return a + b;
      }

      function validatorFunction(func, ...types) {
        const proxy = new Proxy(func, {
          apply(target, thisArgument, argumentsList) {
            types.forEach((t, i) => {
              const arg = argumentsList[i];
              if (typeof arg !== t) {
                throw new TypeError(`第${i + 1}参数${arg}类型不为${t}`);
              }
            });
            return Reflect.apply(target, thisArgument, argumentsList);
          },
        });
        return proxy;
      }

      const sumProxy = validatorFunction(sum, "number", "number");
      console.log(sumProxy(1, "2"));
    </script>
  </body>
</html>
